package cn.cai.pethome.dept.service;

import cn.cai.pethome.PetHomeApp;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.test.context.junit4.SpringRunner;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@SpringBootTest(classes = PetHomeApp.class)
@RunWith(SpringRunner.class)
public class EailText {

    @Autowired
    private JavaMailSender javaMailSender;

    @Test
    public void test() {
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //设置发送人
        mailMessage.setFrom("2567209139@qq.com");
        //邮件主题
        mailMessage.setSubject("新型冠状病毒防护指南");
        //邮件内容
        mailMessage.setText("涛涛，我是飞飞！！好好在家待着.....");
        //收件人
        mailMessage.setTo("1007068665@qq.com");

        javaMailSender.send(mailMessage);
    }
    @Test
    public void test2() throws Exception {
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("2567209139@qq.com");
        helper.setSubject("邮件标题");
        //第二个参数 true 代表是否是一个html内容
        helper.setText("<h1>涛涛，我是飞飞</h1>"+
                "<img src='http://123.207.27.208/group1/M00/01/7C/CgAIC2MKzxWAMDgTAAIsyhup4R8178.jpg\"' />",true);
        //添加附件
        helper.addAttachment("520.jpg",new File("D:\\Newidea\\520.jpg"));
       /* helper.addAttachment("压缩文件", new File("C:\\resources.zip"));*/
        //收件人
        helper.setTo("1007068665@qq.com");
        //抄送
       /* helper.setCc();*/
        //后端密送
       /* helper.addBcc();*/
        //发送邮件
        javaMailSender.send(mimeMessage);
    }
}
